const https = require('https')    //node自带的请求模块
const cheerio = require('cheerio')  //用来解析html
const fs = require('fs')

//请求豆瓣 top250 的页面
https.get('https://movie.douban.com/top250', (res) => {
  // console.log(res);

  let html = ''
  res.on('data', (chunk) => {   //拿到响应体了吗?
    // console.log(chunk.toString());
    html += chunk
  })
  res.on('end', () => {  //当数据彻底拿完
    // console.log(html);

    //开始解析 HTML 里的内容
    const $ = cheerio.load(html)
    const allData = []

    $('.grid_view .item').each(function () {
      const title = $('.title', this).first().text()
      const star = $('.rating_num', this).text()
      const pic = $('.pic a img', this).attr('src')
      // console.log(title);
      // console.log(star);
      // console.log(pic);
      allData.push({ title, star, pic })
    })

    // console.log(allData);
    //保存这份数据
    fs.writeFile('./file.json', JSON.stringify(allData), () => {
      console.log('文件保存成功');

    })
  })

})